#include "maintain_tablemodel.h"

MaintainTableModel::MaintainTableModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}

MaintainTableModel::~MaintainTableModel()
{
}

int MaintainTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_vecRowData.size();
}

int MaintainTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return m_sHorizontalHeader.count();
}

QVariant MaintainTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (role == Qt::DisplayRole) {
        int nCol = index.column();
        int nRow =  index.row();
        QStringList values = m_vecRowData.at(nRow);

        if(values.size() > nCol) {
            return values.at(nCol);
        }
    }
    else if(role == Qt::TextAlignmentRole) {
        if(0 == index.column() || 2 == index.column() || 3 == index.column()) {
            return Qt::AlignCenter;
        }
    }

    return QVariant();
}

Qt::ItemFlags MaintainTableModel::flags(const QModelIndex &index) const
{
    if (!index.isValid()) {
        return Qt::NoItemFlags;
    }

    Qt::ItemFlags flag = QAbstractItemModel::flags(index);

    flag &= (~Qt::ItemIsEditable & ~Qt::ItemIsSelectable);
    return flag;
}

void MaintainTableModel::setHorizontalHeader(const QStringList &headers)
{
    m_sHorizontalHeader =  headers;
}

QVariant MaintainTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
        return m_sHorizontalHeader.at(section);
    }

    return QAbstractTableModel::headerData(section, orientation, role);
}

void MaintainTableModel::setDataVector(const QVector<QStringList> &vecData)
{
    m_vecRowData = vecData;
}
